<?php
/**
 * Description of newPHPClass
 *
 * @author dmitryy
 */
class Models_Tags extends Access_Db {
    function setTag($arr)
    {
        $query = "INSERT INTO " . $this->lang . "_tags (name) VALUES (?)";
        $this->prepare($query);
        $this->insert($arr);
        $id = $this->getLAstInsertedID();
        $query = "INSERT INTO role_permissions (role_id, article_id, action, controller) VALUES (?,?,?,?)";
        $permissionArr = array(
            '1',  //1 - guest group !!!!!!!!!!!!!!!!!!!!!!!
            $id,
            'tag',
            'articles'
        );
        $roleId = $this->getSpecialUserGroup($_COOKIE['user_id']);
        $permissionArr1 = array(
            $roleId['role_id'],
            $id,
            'edittag',
            'articles'
        );
        $permissionArr2 = array(
            $roleId['role_id'],
            $id,
            'editingtag',
            'articles'
        );
        $permissionArr3 = array(
            $roleId['role_id'],
            $id,
            'removetag',
            'articles'
        );
        $permissionArr4 = array(
            $roleId['role_id'],
            $id,
            'removingtag',
            'articles'
        );
        $permissionArr5 = array(
            '1', // 1- is the id of guest group
            $id,
            'tag',
            'articles'
        );
        $this->prepare($query);
        $this->insert($permissionArr);
        $this->insert($permissionArr1);
        $this->insert($permissionArr2);
        $this->insert($permissionArr3);
        $this->insert($permissionArr4);
        $this->insert($permissionArr5);
    }
    
    function validateTag($arr)
    {
        $errors = array();
        if(($arr['0'] == '') or ($arr['0'] == ' '))
        {
            $errors[] = "Fieald is empty.";
            return $errors;
        }
        return FALSE;
    }

    function getTag($id)
    {
        $select = array('id','name');
        $from = $this->lang . '_tags';
        $where = "id='$id'";
        $this->select($select, $from, $where);
        return $this->getSelected();
    }
    
    function getAllTags()
    {
        $select = array('id', 'name');
        $from = $this->lang . '_tags';
        $this->select($select, $from);
        $result = array();
        while ($row = $this->getSelected())
        {
            $result[] = $row;
        }
        return $result;
    }
    
    function editTag($id, $arr)
    {
        $query = "UPDATE " . $this->lang . "_tags SET name='" . $arr['0'] . "' where id='$id'";
        $this->execQuery($query);
    }
    
    function getArticleTags($id)
    {
        $select = array(
            $this->lang . '_tags.id',
            $this->lang . '_tags.name'
        );
        $from = $this->lang . '_tags, ' . $this->lang . '_article_tag';
        $where = $this->lang . '_tags.id=' . $this->lang . '_article_tag.tag_id and ' . $this->lang . '_article_tag.article_id=\'' . $id . '\'';
        $this->select($select, $from, $where);
        $result = array();
        while ($row = $this->getSelected())
        {
            $result[] = $row;
        }
        return $result;
    }

    function getArticlesByTag($id)
    {
        $select = array(
            $this->lang . '_articles.title',
            $this->lang . '_articles.body',
            $this->lang . '_articles.modified',
            $this->lang . '_articles.id'
        );
        $from = $this->lang . '_articles, ' . $this->lang . '_article_tag';
        $where = $this->lang . "_articles.id=" . $this->lang . "_article_tag.article_id AND " . $this->lang . "_article_tag.tag_id='$id'";
        $this->select($select, $from, $where);
        $result = array();
        while ($row = $this->getSelected())
        {
            $result[] = $row;
        }
        return $result;
    }
    
    function setTagToArticle($articleId, $tagId)
    {
        $query = "INSERT INTO " . $this->lang . "_article_tag (article_id, tag_id) VALUES (?,?)";
        $arr = array(
            $articleId,
            $tagId
        );
        $this->prepare($query);
        $this->insert($arr);
    }
    
    function removeTagFromArticle($articleId, $tagId)
    {
        $query = "DELETE FROM " . $this->lang . "_article_tag WHERE article_id='$articleId' AND tag_id='$tagId'";
        $this->execQuery($query);
    }
    
    function getArticlesCount($id)
    {
        $select = array(
            'count(id)'
        );
        $from = $this->lang . '_article_tag';
        $where = "tag_id='$id'";
        $this->select($select, $from, $where);
        return $this->getSelected();
    }
}